package net.mingsoft.game.action;

import net.mingsoft.game.biz.IUserBiz;
import java.util.List;

import net.mingsoft.base.entity.ResultData;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.stream.Collectors;

import org.springframework.web.bind.annotation.*;

import net.mingsoft.game.entity.UserEntity;

import net.mingsoft.basic.util.BasicUtil;
import net.mingsoft.basic.util.StringUtil;
import net.mingsoft.basic.bean.EUListBean;
import net.mingsoft.basic.annotation.LogAnn;
import net.mingsoft.basic.constant.e.BusinessTypeEnum;


import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Hidden;
import io.swagger.v3.oas.annotations.enums.ParameterIn;

/**
* 客户管理控制层
* @author 浩浩～
* 创建日期：2025年6月24日 下午4:00:07<br/>
* 历史修订：<br/>
*/
@Tag(name = "后台-客户接口")
@Controller("gameUserAction")
@RequestMapping("/${ms.manager.path}/game/user")
public class UserAction extends net.mingsoft.game.action.BaseAction{


    /**
    * 注入客户业务层
    */
    @Autowired
    private IUserBiz userBiz;

    /**
    * 返回主界面index
    */
    @Hidden
    @GetMapping("/index")
    public String index() {
        return "/game/user/index";
    }


    /**
    * 返回编辑界面user的form
    */
    @Hidden
    @GetMapping("/form")
    public String form() {
        return "/game/user/form";
    }


    /**
    * 查询客户列表
    * @param user 客户实体
    */
    @Operation(summary = "查询客户列表接口")
    @Parameters({
        @Parameter(name = "xl", description = "玄乐账号", in = ParameterIn.QUERY),
        @Parameter(name = "moneyLz", description = "金钱", in = ParameterIn.QUERY),
        @Parameter(name = "selectCodes", description = "区长", in = ParameterIn.QUERY),
    })
    @RequestMapping(value ="/list",method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public ResultData list(@ModelAttribute @Parameter(hidden = true) UserEntity user) {
        BasicUtil.startPage();
        List userList = null;
        if ( user.getSqlWhere() != null){
            userList = userBiz.query(user);
        } else {
            LambdaQueryWrapper<UserEntity> wrapper = new LambdaQueryWrapper<>(user).orderByDesc(UserEntity::getCreateDate);
            userList = userBiz.list(wrapper);
        }
        return ResultData.build().success(new EUListBean(userList,(int)BasicUtil.endPage(userList).getTotal()));
    }


    /**
    * 获取客户
    * @param user 客户实体
    */
    @Operation(summary = "获取客户列表接口")
    @Parameter(name = "id", description = "主键ID", required =true,in = ParameterIn.QUERY)
    @GetMapping("/get")
    @ResponseBody
    public ResultData get(@ModelAttribute @Parameter(hidden = true) UserEntity user) {
        if (StringUtils.isBlank(user.getId())) {
            return ResultData.build().error(getResString("err.error",this.getResString("id")));
        }
        UserEntity _user = (UserEntity)userBiz.getById(user.getId());
        return ResultData.build().success(_user);
    }


    /**
    * 保存客户
    * @param user 客户实体
    */
    @Operation(summary = "保存客户列表接口")
    @Parameters({
        @Parameter(name = "xl", description = "玄乐账号", required = false, in = ParameterIn.QUERY),
        @Parameter(name = "moneyLz", description = "金钱", required = false, in = ParameterIn.QUERY),
        @Parameter(name = "selectCodes", description = "区长", required = false, in = ParameterIn.QUERY),
    })
    @PostMapping("/save")
    @ResponseBody
    @LogAnn(title = "保存客户", businessType = BusinessTypeEnum.INSERT)
    @RequiresPermissions("game:user:save")
    public ResultData save(@ModelAttribute @Parameter(hidden = true) UserEntity user) {

        if (  StringUtils.isNotBlank(user.getXl()) && !StringUtil.checkLength(user.getXl()+"", 0, 255)) {
          return ResultData.build().error(getResString("err.length", this.getResString("xl"), "0", "255"));
        }
        userBiz.save(user);
        return ResultData.build().success(user);
    }

    /**
    *  删除客户
    *
    * @param users 客户实体
    */
    @Operation(summary = "批量删除客户列表接口")
    @PostMapping("/delete")
    @ResponseBody
    @LogAnn(title = "删除客户", businessType = BusinessTypeEnum.DELETE)
    @RequiresPermissions("game:user:del")
    public ResultData delete(@RequestBody List<UserEntity> users) {
        List<String> ids = (List)users.stream().map((p) -> {return p.getId();}).collect(Collectors.toList());
        return this.userBiz.removeByIds(ids) ? ResultData.build().success() : ResultData.build().error(this.getResString("err.error", new String[]{this.getResString("id")}));
    }

    /**
    *	更新客户列表
    *
    * @param user 客户实体
    */
    @Operation(summary = "更新客户列表接口")
    @Parameters({
        @Parameter(name = "id", description = "主键ID", required =true,in = ParameterIn.QUERY),
        @Parameter(name = "xl", description = "玄乐账号", required = false, in = ParameterIn.QUERY),
        @Parameter(name = "moneyLz", description = "金钱", required = false, in = ParameterIn.QUERY),
        @Parameter(name = "selectCodes", description = "区长", required = false, in = ParameterIn.QUERY),
    })
    @PostMapping("/update")
    @ResponseBody
    @LogAnn(title = "更新客户", businessType = BusinessTypeEnum.UPDATE)
    @RequiresPermissions("game:user:update")
    public ResultData update(@ModelAttribute @Parameter(hidden = true) UserEntity user) {
        //先查询数据是否存在
        UserEntity _user = (UserEntity)userBiz.getById(user.getId());
        if(_user == null) {
            return ResultData.build().error(getResString("err.not.exist",user.getId() ));
        }

        if (  StringUtils.isNotBlank(user.getXl()) && !StringUtil.checkLength(user.getXl()+"", 0, 255)) {
          return ResultData.build().error(getResString("err.length", this.getResString("xl"), "0", "255"));
        }
        userBiz.updateById(user);
        return ResultData.build().success(user);
    }

    @GetMapping("verify")
    @ResponseBody
    public ResultData verify(String fieldName, String fieldValue, String id,String idName) {
        boolean verify = false;
        if (StringUtils.isBlank(id)) {
            verify = super.validated("GAME_USER",fieldName,fieldValue);
        } else {
            verify = super.validated("GAME_USER",fieldName,fieldValue,id,idName);
        }
        if (verify) {
            return ResultData.build().success(false);
        }else {
            return ResultData.build().success(true);
        }
    }

}